package com.lagou.cl.driver;

import com.lagou.cl.mapper.SortMapper;
import com.lagou.cl.reduce.SortReduce;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class SortDriver {

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

		String projectPath = System.getProperty("user.dir");

		Configuration configuration = new Configuration();
		Job job = Job.getInstance(configuration);

		job.setJarByClass(SortDriver.class);

		job.setMapperClass(SortMapper.class);
		job.setReducerClass(SortReduce.class);

		job.setMapOutputKeyClass(IntWritable.class);
		job.setMapOutputValueClass(NullWritable.class);

		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);

		FileInputFormat.setInputPaths(job, new Path(projectPath + "\\mapreduce-work\\src\\main\\resources\\file"));
		FileOutputFormat.setOutputPath(job, new Path(projectPath + "\\mapreduce-work\\src\\main\\resources\\" + System.currentTimeMillis()));

		boolean result = job.waitForCompletion(true);

		//JVM退出，0是正常退出，非0是错误退出
		System.exit(result ? 0 : 1);
	}
}
